import Sdk from "../../lib/Sdk";
import { TipManager } from "../../lib/TipManager";
import {ui} from "../../ui/layaMaxUI";
import ItemBaseData from "../data/ItemBaseData";
import PlayerData from "../data/PlayerData";
export default class AwardDialog extends ui.AwardDialogUI{
    private mdata:Array<ItemBaseData>;
    private mPer:number=1;
    constructor(){
        super();
        this.list.array=[];
        this.list.renderHandler=new Laya.Handler(this,this.rederrr);
    }

    private rederrr(_sp:Laya.Sprite,_index:number){
        let tdata:ItemBaseData=this.mdata[_index];
        _sp.getChildByName("icon")["skin"]=tdata.icon;
        _sp.getChildByName("numTxt")["text"]="x"+tdata.num;
    }
    onOpened(_data:Array<ItemBaseData>){
        this.mdata=_data;
        this.fresh();
        this.videoBtn.on(Laya.Event.CLICK,this,this.videoClick);
        this.okBtn.on(Laya.Event.CLICK,this,this.close);
        this.closeBtn.on(Laya.Event.CLICK,this,this.close);
        this.mPer=1;
        this.tipTxt.visible=false;
        Sdk.getIns().showBanner();
    }

    onDisable(){
        this.videoBtn.off(Laya.Event.CLICK,this,this.videoClick);
        this.okBtn.off(Laya.Event.CLICK,this,this.close);
        this.closeBtn.off(Laya.Event.CLICK,this,this.close);
        Sdk.getIns().hideBanner();
        this.save();
    }

    private videoClick(){
        Sdk.getIns().showAd((_code:number)=>{
            if(_code==0){
                this.mPer=2;
                this.tipTxt.visible=true;
                this.videoBtn.visible=false;
                this.doulbe();
            }else{
                TipManager.ins.showMsgAlert("观看完整视频才能翻倍哦",this);
            }
        });
    }

    private fresh(){
        this.list.array=this.mdata;
        let tx=95;
        if(this.mdata.length==3)tx=160;
        else if(this.mdata.length==2)tx=229;
        else if(this.mdata.length==1)tx=294;
        this.list.x=tx;
    }

    private doulbe(){
        for(let i=0;i<this.mdata.length;i++){
            this.mdata[i].num *= this.mPer;
        }
        this.list.array=this.mdata;
    }

    private save(){
        let tdata:ItemBaseData;
        for(let i=0;i<this.mdata.length;i++){
            tdata = this.mdata[i];
            PlayerData.getIns().addItem(tdata.id+"",tdata.num*this.mPer);
        }
    }
}